<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/27
 * Time: 12:04
 */

namespace app\admin\controller;


use app\admin\model\AdminMenu;

class MenuController extends BaseController
{

    public function menuList(){
        $data['list'] = AdminMenu::order('pid asc')->paginate(10)->each(function ($item){
            $ret = AdminMenu::where('id',$item->pid)->field('menu_name')->find();
            if ($ret){
                $item->pid = $ret->menu_name;
            }else{
                $item->pid = '一级菜单';
            }
        });
        $this->assign('data',$data);
        return $this->fetch();
    }

    public function menuEdit($id){
        $data['info'] = AdminMenu::get((int)$id);
        $data['pid']  = AdminMenu::where('pid',0)->field('id,menu_name')->select()->toArray();
//        dump($data);
        $this->assign('data',$data);
        return $this->fetch();
    }

    public function menuSave(){
        $id = (int)input('post.id');
        $menu_name = trim(input('post.menu_name'));
        $pid = (int)input('post.pid');
        $status = (int)input('post.status');
        $upData = [
            'menu_name'=>$menu_name,
            'pid'=>$pid,
            'status'=>$status,
        ];
        $ret = AdminMenu::update($upData,['id'=>$id]);
        if (!$ret){
            return json(['code'=>0,'msg'=>'修改失败']);
        }
        return json(['code'=>1,'msg'=>'修改成功']);
    }



}